import { Page } from '@/base/Page'
import { deepAssign } from '@/base/util/Objects';
import { SeftMotion } from '@/base/model/SeftMotion';
import { DeviceSort } from '@/base/model/DeviceSort';
import { DeviceModel } from '@/base/model/DeviceModel';
import {
  deleteSeftMotionId,
  getSeftMotion,
  getSeftMotionId,
  postSeftMotion,
  PutSeftMotionId,
  getVersions
} from '@/base/api/SeftMotionApi'
import { getDeviceModel, getDeviceModelId } from '@/base/api/DeviceModelApi'
import { getDeviceSort } from '@/base/api/DeviceSortApi'

export class AutomaticBloc {

  page: Page<DeviceSort> = new Page<DeviceSort>([], 0, 1, 15, 0, []);
  page1: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 15, 0, []);
  seftMotion: Page<SeftMotion> = new Page<SeftMotion>([], 0, 1, 10, 0, []);
  model: any = {
    describe: '',
    extName: '',
    iconUrl: '',
    selectedIconUrl: '',
    backgroundUrl: '',
    appUrl: '',
    modeName: '',
    modeNameEn: '',
    sn: '',
    modelId: 0,
    sortId: 0,
    url: '',
    versions: '',
    issueStatus: '',
    accountName: '',
    text: '',
    classification: '',
    describeEn: '',
  };
  seftMotionId: SeftMotion = {
    accountId: 0,
    accountName: '',
    created: '',
    describe: '',

    extName: '',
    iconUrl: '',
    selectedIconUrl: '',
    backgroundUrl: '',
    appUrl: '',
    id: 0,
    issueStatus: '',
    modeName: '',
    modeNameEn: '',
    sn: '',
    modelId: 0,
    sortId: 0,
    updated: '',
    url: '',
    versions: '',
    text: '',
    classification: '',
    describeEn: '',
  }
  sortList: any = [];
  modelList: any = [];
  agentList: any = [];
  versions: any = [];
  fileList: any = [];
  fileSelectIcon: any = [];
  fileListBackground: any = [];
  appList: any = [];
  loading = false;
  initContent = '';
  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getDeviceSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      deepAssign(this.page, res.data);
      this.sortList = this.page.items.map(function (item) {
        var obj = { id: 0, sortName: '' };
        // @ts-ignore
        obj.id = item.id;
        obj.sortName = item.sortName;
        return obj
      });
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getAllModel(pageIndex: number, pageSize: number, sortId: number, sortDirection: string, companyId) {
    this.loading = true;
    let that = this;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId,
      id: this.model.id,
      modelName: this.model.modeName
    }).then((res) => {
      deepAssign(this.page1, res.data);
      this.modelList = this.page1.items.map(function (item) {
        var obj = { id: 0, modelName: '' };
        obj.id = item.id;
        obj.modelName = item.modelName;
        return obj
      });
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getModel(id: number) {
    this.loading = true;
    return getDeviceModelId({ id }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
  // this.bloc.model.describe,
  // this.bloc.model.iconUrl,
  // this.bloc.model.selectedIconUrl,
  // this.bloc.model.backgroundUrl,
  // this.bloc.model.appUrl,
  // this.bloc.model.modeName,
  // this.bloc.model.modeNameEn,
  // this.bloc.model.sn,
  // this.bloc.model.modelId,
  // this.bloc.model.sortId,
  // this.bloc.model.url,
  // this.bloc.model.issueStatus,
  // this.bloc.model.describeEn,
  // this.bloc.model.classification
  addSeftMotion(
    describe: string,
    iconUrl: string,
    selectedIconUrl: string,
    backgroundUrl: string,
    appUrl: string,
    modeName: string,
    modeNameEn: string,
    sn: string,
    modelId: number,
    sortId: number,
    url: string,
    issueStatus: string,
    classification: string,
    describeEn: string,) {
    this.loading = true;
    return postSeftMotion({
      describe,
      iconUrl,
      selectedIconUrl,
      backgroundUrl,
      appUrl,
      modeName,
      modeNameEn,
      sn,
      modelId,
      sortId,
      url,
      extName: this.model.extName,
      issueStatus,
      classification,
      describeEn,
      text: this.model.text,
    }).then((res) => {
      this.getSeftMotion(this.seftMotion.pageIndex, 10, 'DESC', this.model
      )
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getSeftMotion(
    pageIndex: number, pageSize: number, sortDirection: string, model: any) {
    this.loading = true;
    return getSeftMotion({
      pageIndex: pageIndex,
      pageSize: pageSize,
      sortDirection: sortDirection,
      accountName: model.accountName,
      versions: model.versions,
      issueStatus: model.issueStatus,
      modelId: model.modelId
    }).then((res) => {
      // console.log(res.data);
      deepAssign(this.seftMotion, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getVersions() {
    this.loading = true;
    return getVersions().then((res) => {
      this.versions = res.data.map(function (item, index) {
        let obj = { index: 0, version: '' }
        obj.index = index
        // @ts-ignore
        obj.version = item
        return obj
      })
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  toModifyItem(id: number) {
    this.loading = true;
    return getSeftMotionId(
      id
    ).then((res) => {
      deepAssign(this.seftMotionId, res.data);
      this.initContent = res.data.text;
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  modifyItem(describe: string,
    describeEn: string,
    iconUrl: string,
    selectedIconUrl: string,
    backgroundUrl: string,
    appUrl: string,
    id: number,
    issueStatus: string,
    modeName: string,
    modeNameEn: string,
    sn: string,
    modelId: number,
    sortId: number,
    url: string,
    classification: string) {
    this.loading = true;
    console.log(appUrl);
    return PutSeftMotionId({
      describe,
      describeEn,
      extName: this.seftMotionId.extName,
      iconUrl,
      selectedIconUrl,
      backgroundUrl,
      appUrl,
      id,
      issueStatus,
      modeName,
      modeNameEn,
      sn,
      modelId,
      sortId,
      url,
      text: this.seftMotionId.text,
      classification
    }).then((res) => {
      deepAssign(this.seftMotionId, res.data);
      this.getSeftMotion(this.seftMotion.pageIndex, 10, 'DESC', this.model)
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  removeItem(dateItem: SeftMotion) {
    this.loading = true;
    return deleteSeftMotionId(dateItem.id)
      .then((res) => {
        this.getSeftMotion(this.seftMotion.pageIndex, 10, 'DESC', this.model)
        return res;
      })
      .finally(() => {
        this.loading = false;
      })
  }

}
